package com.yohann.algorithm;

import sun.misc.Queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 图演示
 * </p>
 *
 * @author Yohann
 * @since 2021/1/12 21:28
 */
public class GraphDemo {
    public static void main(String[] args) {
        String[] node = {"A", "B", "C", "D", "E"};
        Graph graph = new Graph(5);
        for (String s : node) {
            graph.insertVertex(s);
        }

        graph.insertEdge(0, 1, 1);
        graph.insertEdge(0, 2, 1);
        graph.insertEdge(1, 2, 1);
        graph.insertEdge(1, 3, 1);
        graph.insertEdge(1, 4, 1);

        graph.showGraph();

//        graph.dfs();
        graph.bfs();
    }
}

class Graph {
    private List<String> vertexList;
    private int[][] edges;
    private int numOfEdges;
    private boolean[] isVisited;

    public Graph(int n) {
        vertexList = new ArrayList<>(n);
        this.edges = new int[n][n];
        numOfEdges = 0;
        isVisited = new boolean[n];
    }

    //插入节点
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }

    /**
     * 添加边
     *
     * @param v1     表示第几个顶点
     * @param v2     第二个顶点的
     * @param weight 权值
     */
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }

    //返回节点个数
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //返回边的个数
    public int getNumOfEdges() {
        return numOfEdges;
    }

    //获取节点
    public String getVertex(int i) {
        return vertexList.get(i);
    }

    //获取权值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //遍历
    public void showGraph() {
        for (int[] edge : edges) {
            System.out.println(Arrays.toString(edge));
        }
    }

    //深度优先遍历
    private void dfs(boolean[] isVisited, int i) {
        if (i == vertexList.size() - 1) {
            System.out.print(getVertex(i));
        } else {
            System.out.print(getVertex(i) + "->");
        }

        isVisited[i] = true;

        int neighbor = getFirstNeighbor(i);
        while (neighbor != -1) {
            if (!isVisited[neighbor]) {
                dfs(isVisited, neighbor);
            }

            //寻找下一个节点
            neighbor = getNextNeighbor(i, neighbor);
        }
    }

    //遍历所有节点 回溯
    public void dfs() {
        for (int i = 0; i < getNumOfVertex(); i++) {
            if (!isVisited[i]) {
                dfs(isVisited, i);
            }
        }
    }

    //获取到第一个邻接点的下标w
    public int getFirstNeighbor(int index) {
        for (int i = index; i < vertexList.size(); i++) {
            if (edges[index][i] > 0) {
                return i;
            }
        }

        return -1;
    }

    //获取下一个邻接点的下标
    public int getNextNeighbor(int v1, int v2) {
        for (int i = v2 + 1; i < vertexList.size(); i++) {
            if (edges[v1][i] > 0) {
                return i;
            }
        }

        return -1;
    }

    //广度优先算法
    private void bfs(boolean[] isVisited, int i) {
        //队列头节点
        int u;
        //邻接点下标
        int w;
        LinkedList<Integer> queue = new LinkedList<>();

        if (i == vertexList.size() - 1) {
            System.out.print(getVertex(i));
        } else {
            System.out.print(getVertex(i) + "->");
        }

        isVisited[i] = true;
        queue.add(i);

        while (!queue.isEmpty()) {
            u = queue.removeFirst();
            w = getFirstNeighbor(u);

            while (w != -1) {
                //是否访问过
                if (!isVisited[w]) {
                    if (i == vertexList.size() - 1) {
                        System.out.print(getVertex(i));
                    } else {
                        System.out.print(getVertex(i) + "->");
                    }
                    isVisited[w] = true;
                    queue.add(w);
                }

                //找下一个节点
                w = getNextNeighbor(u, w);
            }
        }
    }

    public void bfs() {
        for (int i = 0; i < getNumOfVertex(); i++) {
            if (!isVisited[i]) {
                dfs(isVisited, i);
            }
        }
    }
}